package com.ostrich.demomvvm.ui.home

import android.util.Log
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.ostrich.demomvvm.R
import com.ostrich.demomvvm.databinding.FragmentHomeBinding
import com.ostrich.demomvvm.mvvm.BaseMvvmFragment
import com.ostrich.demomvvm.ui.home.adapter.VideoAdapter
import com.ostrich.demomvvm.ui.home.entity.VideoEntity
import com.ostrich.demomvvm.ui.home.viewmodel.HomeViewModel


class HomeFragment : BaseMvvmFragment<HomeViewModel,FragmentHomeBinding>() {
    private lateinit var videoAdapter: VideoAdapter
    private lateinit var layoutManager: StaggeredGridLayoutManager
    override fun bindLayout(): Int {
        return R.layout.fragment_home
    }

    override fun initView() {
        videoAdapter = VideoAdapter()
        layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
        binding?.let {
            it.rv.adapter = videoAdapter
            it.rv.layoutManager = layoutManager
            it.rv.setHasFixedSize(true) //recyclerview优化：item高度固定，不需要重新执行layout摆放

        }

    }

    override fun initData() {
        viewModel.findVideos(1,20).observe(this){
            if(it.code == 200){
                val data = it.data
                videoAdapter.setData(data)
            }
        }

    }

    override fun initListener() {

    }

    override fun getJVMClass(): Class<HomeViewModel> {
        return HomeViewModel::class.java
    }

}